package com.aplana.ccsd.service;

import info.magnolia.cms.core.MgnlNodeType;
import info.magnolia.cms.util.ContentUtil;
import info.magnolia.jcr.util.NodeUtil;
import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.module.blossom.dialog.TabBuilder;
import info.magnolia.module.templatingkit.dam.Asset;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aplana.ccsd.enums.CertificateType;
import com.aplana.ccsd.model.Certificate;
import com.aplana.ccsd.util.DialogUtils;
import com.aplana.ccsd.util.JcrNodeUtils;

@SuppressWarnings("deprecation")
@Service
public class CertificateServiceImpl implements CertificateService {

	@Autowired
	private TemplatingService templatingService;

	@Override
	public Certificate getCertificate(Node certificatePage)
			throws RepositoryException {
		String title = PropertyUtil.getString(certificatePage, "title");
		CertificateType type = CertificateType.getCertificateType(PropertyUtil
				.getString(certificatePage, "certificateType"));

		Certificate certificate = new Certificate(title, type,
				JcrNodeUtils.getURL(certificatePage));

		String comment = templatingService.getNodePropertyAsHtml(
				certificatePage, "comment");
		Asset image = templatingService.getNodePropertyAsImage(certificatePage,
				"image");

		certificate.setComment(comment);
		certificate.setImage(image);
		return certificate;
	}

	@Override
	public void addCertificateDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Заголовок", "");
		// options where key is label and value is value
		Map<String, String> typeOptions = new LinkedHashMap<String, String>();
		for (CertificateType type : CertificateType.values()) {
			typeOptions.put(type.getValue(), type.getId());
		}
		tabBuilder.addRadio("certificateType", "Тип", typeOptions,
				CertificateType.CERTIFICATE.getId());
		DialogUtils.addDamControl(tabBuilder, "image", "Скан сертификата", "");

		tabBuilder.addFckEditor("comment", "Комментарий", "");
	}

	@Override
	public void addCertificateListDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Title", "");
		tabBuilder.addLink("certificatesLink", "Страница с сертификатами",
				"Ссылка на страницу с сертификатами");
	}

	@Override
	public Node getCertificateListPage(Node component)
			throws RepositoryException {
		String nodeLink = PropertyUtil.getString(component, "certificatesLink");

		Node articlesPage = component.getSession().getNode(nodeLink);
		return articlesPage;
	}

	@Override
	public List<Certificate> findCertificates(Node certificatesParentPage)
			throws RepositoryException {
		Iterable<Node> pages = NodeUtil
				.getNodes(certificatesParentPage, MgnlNodeType.NT_PAGE);
		List<Certificate> results = new ArrayList<Certificate>();
		for (Node page : pages) {
			if (!ContentUtil.asContent(page).hasMixin(MgnlNodeType.MIX_DELETED)) {
				Certificate item = getCertificate(page);
				results.add(item);
			}
		}
		return results;
	}

}